//app.js
App({
  onLaunch: function () {
    showRequestInfo()
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  checkLogin:function(cb){
    var that=this;
    var sessionId='';
    try{
      sessionId = wx.getStorageSync('pi_wxapp_sessionId')
    }catch(e){

    }
  
    if (sessionId==null || sessionId==''){
      that.login(cb);
      return;
    }

    wx.request({
                url: that.globalData.serverUrl+'/app/mp',
                method:'post',
                dataType:'json',
                data: {
                  messageId:"verifySession",
                  sessionId: sessionId,
                  body: {
                    
                  }
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res1) {

                  if(!res1.data.success){
                     that.login(cb);
                  }else{
                    wx.checkSession({
                      success: function(){
                        
                      },
                      fail: function(){
                        //登录态过期
                        that.login(cb)
                      }
                    })
                  }
                }
              })

    
  },
  login:function(cb){
      var that=this
      wx.login({
          success: function(res) {
            //获取用户基础信息
            wx.getUserInfo({
              success: function (res) {
                that.globalData.userInfo = res.userInfo
                typeof cb == "function" && cb(that.globalData.userInfo)
              }
            })
            
            if (res.code) {
              //发起网络请求
              wx.request({
                url: that.globalData.serverUrl+'/app/mp',
                method:'post',
                dataType:'json',
                data: {
                  messageId: 'wxLogin',
                  body: {
                    code:res.code
                  }
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res1) {
                  if(res1.data.success){
                    wx.setStorage({
                      key:"pi_wxapp_sessionId",
                      data:res1.data.sessionId
                    })
                  }
                }
              })
            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          }
        })
  },
  globalData:{
    userInfo:null,
    serverUrl:'https://store.pencil-island.com',
    // serverUrl: 'http://101.200.212.209:19999',
  }
})